package com.rainwood.water_authority.ui.activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.rainwood.water_authority.R;
import com.rainwood.water_authority.base.MyBaseActivity;
import com.rainwood.water_authority.helper.InputTextHelper;
import com.rainwood.water_authority.http.request.InventoryIssueApi;
import com.rainwood.water_authority.http.request.InventoryMaterialApi;
import com.rainwood.water_authority.http.response.PanDianHistoryBean;

import com.rainwood.water_authority.http.response.SimpleResultBean;
import com.rainwood.water_authority.other.IntentKey;
import com.rainwood.water_authority.ui.adapter.InventoryIssueBaseAdapter;
import com.rainwood.water_authority.ui.adapter.HistoryPanDianAdapter;
import com.rainwood.water_authority.ui.dialog.MessageDialog;
import com.rainwood.water_authority.util.SoftHideKeyBoardUtils;
import com.sxs.easyhttp.EasyHttp;
import com.sxs.easyhttp.listener.HttpCallback;
/**
 * @author sxs
 * @date :2020/9/28
 * @desc: 盘点物资数量
 */
public final class PanDianDetilsAct extends MyBaseActivity {
    public static void start(Context context, String goodsId, String type) {
        Intent intent = new Intent(context, PanDianDetilsAct.class);
        intent.putExtra(IntentKey.ID, goodsId);
        intent.putExtra(IntentKey.TYPE, type);
        context.startActivity(intent);
    }

    private RecyclerView mOutboundListView;
    private EditText mOutboundNumView;

    private TextView mIssueNameView;
    private TextView mApplyNumView;
    private TextView mIssueNoView;
    private TextView mModelView;
    private TextView mUnitView, tv_apply_num02, btn_outbound;
    private InventoryIssueBaseAdapter mInventoryIssueAdapter;
    private HistoryPanDianAdapter historyPanDianAdapter;
    private LinearLayout btn_scan_outbound;
    private PanDianHistoryBean panDianHistoryBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_inventory_issue;
    }

    @Override
    protected void initView() {
        mIssueNameView = (TextView) findViewById(R.id.tv_issue_name);
        mApplyNumView = (TextView) findViewById(R.id.tv_apply_num);
        mIssueNoView = (TextView) findViewById(R.id.tv_material_number);
        mModelView = (TextView) findViewById(R.id.tv_model);
        mUnitView = (TextView) findViewById(R.id.tv_measuring_unit);
        tv_apply_num02 = (TextView) findViewById(R.id.tv_apply_num02);
        btn_scan_outbound = findViewById(R.id.btn_scan_outbound);
        btn_outbound = findViewById(R.id.btn_outbound);
        btn_outbound.setVisibility(View.GONE);


        mOutboundListView = findViewById(R.id.rv_outbound_list);
        mOutboundNumView = findViewById(R.id.et_outbound_num);
        btn_scan_outbound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updata();
            }
        });

        mInventoryIssueAdapter = new InventoryIssueBaseAdapter(this);
        historyPanDianAdapter = new HistoryPanDianAdapter(this);
        mOutboundListView.setAdapter(historyPanDianAdapter);
        InputTextHelper.with(this)
                .addView(mOutboundNumView)
                .setMain(btn_scan_outbound)
                .build();

        SoftHideKeyBoardUtils.assistActivity(this);
    }

    private void updata() {
        String numStr = mOutboundNumView.getText().toString().trim();
        int num = Integer.parseInt(numStr);
        if (mInventoryIssueAdapter.getData() != null && Integer.parseInt(panDianHistoryBean.getData().getInfo().getStockNum()) < num) {
            new MessageDialog.Builder(this)
                    .setMessage("当前库存不足")
                    .setCancel(null)
                    .setListener(dialog -> {
                    }).show();
            return;
        }
        if (panDianHistoryBean!=null&&panDianHistoryBean.getData().getInfo()!=null&&panDianHistoryBean.getData().getInfo().getMaterial()!=null){
            // 物资盘点
            EasyHttp.post(this)
                    .api(new InventoryMaterialApi()
                            .setCode(panDianHistoryBean.getData().getInfo().getMaterial().getCode())
                            .setGoodsId(panDianHistoryBean.getData().getInfo().getOrderId())
                            .setNum(num))
                    .request(new HttpCallback<SimpleResultBean>(this) {
                        @Override
                        public void onSucceed(SimpleResultBean result) {
                            toast(result.getWarn());
                            if (result.getCode() == 1) {
                                postDelayed(() -> finish(), 1000);
                            }
                        }
                    });
        }

    }

    @Override
    protected void initData() {
        EasyHttp.post(this)
                .api(new InventoryIssueApi().setGoodsId(getString(IntentKey.ID)))
                .request(new HttpCallback<PanDianHistoryBean>(this) {
                    @Override
                    public void onSucceed(PanDianHistoryBean result) {
                        panDianHistoryBean = result;
                        if (result.getData().getInfo() != null) {
                            mIssueNameView.setText(result.getData().getInfo().getMaterial().getName());
                            mApplyNumView.setText(result.getData().getInfo().getStockNum());
                            tv_apply_num02.setText(result.getData().getInfo().getNum());
                            mIssueNoView.setText(result.getData().getInfo().getMaterial().getCode());
                            mModelView.setText(result.getData().getInfo().getMaterial().getModel());
                            mUnitView.setText(result.getData().getInfo().getMaterial().getUnit());
                        }
                        if (result.getData() != null && result.getData().getRecord() != null) {
                            historyPanDianAdapter.clearData();
                            historyPanDianAdapter.setData(result.getData().getRecord());
                        }
                    }
                });

//        EasyHttp.post(this)
//                .api(("scan".equals(getString(IntentKey.TYPE))
//                        ? new InventoryScanApi().setCsId(getString(IntentKey.ID))
//                        : new InventoryIssueApi().setGoodsId(getString(IntentKey.ID))))
//                .request(new HttpCallback<InventoryIssueBean>(this) {
//                    @Override
//                    public void onSucceed(InventoryIssueBean result) {
//                        if (result.getData() != null) {
//                            if (result.getCode() == 0) {
//                                toast(result.getWarn());
//                                ErrorPageAty.start(getContext(), "盘点扫码失败");
//                                finish();
//                                return;
//                            }
//                           /* if (!"inventory".equals(result.getData().getCType())){
//                                toast("非盘点物资");
//                                postDelayed(() -> finish(), 1000);
//                                return;
//                            }*/
//                            InventoryIssueBean.DataDTO resultData = result.getData();
//                            List<InventoryDetailBean.DataDTO.GoodsDTO> goodsDTOList = new ArrayList<>();
//                            InventoryDetailBean.DataDTO.GoodsDTO goodsDTO = new InventoryDetailBean.DataDTO.GoodsDTO();
//                            goodsDTO.setGoodsId(resultData.getGoodsId());
//                            goodsDTO.setName(resultData.getName());
//                            goodsDTO.setNowKuNum(resultData.getNowKuNum());
//                            goodsDTO.setNumber(resultData.getNumber());
//                            goodsDTO.setSpeciModel(resultData.getSpeciModel());
//                            goodsDTO.setUnit(resultData.getUnit());
//                            goodsDTOList.add(goodsDTO);
//                            mInventoryIssueAdapter.clearData();
//                            mInventoryIssueAdapter.setData(goodsDTOList);
//                        } else {
//                            if (result.getCode() == 0) {
//                                toast(result.getWarn());
//                                ErrorPageAty.start(getContext(), "盘点扫码失败");
//                                finish();
//                            }
//                        }
//                    }
//                });
    }


}
